﻿namespace ECommerce
{
	using System.Linq;
	using System.Web;
	using System.Web.Services;

	/// <summary>
	/// Summary description for $codebehindclassname$
	/// </summary>
	[WebService(Namespace = "http://www.godbuying.com/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	public class NewProducts24_31 : IHttpHandler
	{
		private Service.IProductService _ProductService;

		public void ProcessRequest(HttpContext context)
		{
			context.Response.ContentType = "text/xml";
			context.Response.Write(GenerateXML());
		}

		private string GenerateXML()
		{
			if(_ProductService == null)
			{
				_ProductService = new Service.ProductService();
			}

			if (SiteContext.Current != null && SiteContext.Current.WorkingLanguage != null)
			{
				var languageID = SiteContext.Current.WorkingLanguage.LanguageId;

				var productByLang = _ProductService.GetProductsByLanguage(languageID);

				if (productByLang != null)
				{
					var newProducts24_31 = (from p in productByLang
											where p.Recommend.GetValueOrDefault()
											orderby p.DisplayOrder
											select p).Skip(23).Take(8);

					if (newProducts24_31 != null && newProducts24_31.Count() > 0)
					{
						System.Text.StringBuilder xmlBuilder = new System.Text.StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");

						xmlBuilder.Append("<pic>");

						string imgUrl = @"http://godbuying.com/PictureHandler.ashx?picturesize=155&productid=";
						string navUrl = @"http://godbuying.com/ProductDetail.aspx?productid=";

						foreach (var prod in newProducts24_31)
						{
							xmlBuilder.Append("<myImage>");
							xmlBuilder.Append("<ImageName>");
							xmlBuilder.Append(imgUrl + prod.ProductID.ToString());
							xmlBuilder.Append("</ImageName>");
							xmlBuilder.Append("<ImageUrl>");
							xmlBuilder.Append(navUrl + prod.ProductID.ToString());
							xmlBuilder.Append("</ImageUrl>");
							xmlBuilder.Append("</myImage>");
						}

						xmlBuilder.Append("</pic>");

						return xmlBuilder.ToString();
					}
				}
			}

			return string.Empty;
		}

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}
}
